package com.ruoyi.car.controller;

import com.ruoyi.car.domain.CarGoods;
import com.ruoyi.car.domain.vo.CarGoodsNameVo;
import com.ruoyi.car.service.ICarGoodsService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 车辆管理Controller
 *
 * @author ruoyi
 * @date 2024-04-07
 */
@RestController
@RequestMapping("/car/goods")
public class CarGoodsController extends BaseController {
    @Autowired
    private ICarGoodsService carGoodsService;

    /**
     * 查询车辆管理列表
     */
    @PreAuthorize("@ss.hasPermi('car:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(CarGoods carGoods) {
        startPage();
        List<CarGoods> list = carGoodsService.selectCarGoodsList(carGoods);
        return getDataTable(list);
    }

    /**
     * 查询车辆所有列表vo
     */
    @PreAuthorize("@ss.hasPermi('car:goods:list')")
    @GetMapping("/listAllVo")
    public AjaxResult listAllVo() {
        List<CarGoods> list = carGoodsService.selectCarGoodsList(new CarGoods());
        if (CollectionUtils.isEmpty(list)) {
            return success(list);
        }
        List<CarGoodsNameVo> carGoodsNameVoList =
                list.stream().map(carGoods -> {
                    CarGoodsNameVo vo = new CarGoodsNameVo();
                    BeanUtils.copyProperties(carGoods, vo);
                    return vo;
                }).collect(Collectors.toList());
        return success(carGoodsNameVoList);
    }


    /**
     * 导出车辆管理列表
     */
    @PreAuthorize("@ss.hasPermi('car:goods:export')")
    @Log(title = "车辆管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CarGoods carGoods) {
        List<CarGoods> list = carGoodsService.selectCarGoodsList(carGoods);
        ExcelUtil<CarGoods> util = new ExcelUtil<CarGoods>(CarGoods.class);
        util.exportExcel(response, list, "车辆管理数据");
    }

    /**
     * 获取车辆管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('car:goods:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(carGoodsService.selectCarGoodsById(id));
    }

    /**
     * 新增车辆管理
     */
    @PreAuthorize("@ss.hasPermi('car:goods:add')")
    @Log(title = "车辆管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CarGoods carGoods) {
        return toAjax(carGoodsService.insertCarGoods(carGoods));
    }

    /**
     * 修改车辆管理
     */
    @PreAuthorize("@ss.hasPermi('car:goods:edit')")
    @Log(title = "车辆管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CarGoods carGoods) {
        return toAjax(carGoodsService.updateCarGoods(carGoods));
    }

    /**
     * 删除车辆管理
     */
    @PreAuthorize("@ss.hasPermi('car:goods:remove')")
    @Log(title = "车辆管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(carGoodsService.deleteCarGoodsByIds(ids));
    }
}
